package com.danger.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.andreabaccega.formedittextvalidator.CreditCardValidator;
import com.andreabaccega.formedittextvalidator.EmailValidator;
import com.andreabaccega.formedittextvalidator.OrValidator;
import com.andreabaccega.widget.FormEditText;

public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private FormEditText username_editText;
    private FormEditText password_editText;
    private FormEditText email_editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

    }

    private void initView() {
        username_editText = (FormEditText) findViewById(R.id.username_editText);
        password_editText = (FormEditText) findViewById(R.id.password_editText);
        email_editText = (FormEditText) findViewById(R.id.email_editText);
        button = (Button) findViewById(R.id.button);

        username_editText.addValidator(new MyValidator("username"));
        password_editText.addValidator(new MyValidator("password"));


    }

    private void initEvent() {
        username_editText.setOnFocusChangeListener(this);
        password_editText.setOnFocusChangeListener(this);
        email_editText.setOnFocusChangeListener(this);
    }


    public void onClickValidate(View v) {
        FormEditText[] allFields = {username_editText, password_editText, email_editText};
        boolean allVaild = true;
        for (FormEditText field : allFields) {
            allVaild = field.testValidity() && allVaild;
        }

        if (allVaild) {
            Toast.makeText(MainActivity.this, "validation true", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "validation false", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.username_editText:
                username_editText.testValidity();
                break;
            case R.id.password_editText:
                password_editText.testValidity();
                break;
            case R.id.email_editText:
                email_editText.testValidity();
                break;
            default:
                break;
        }
    }
}
